package de.gwtprime.client.views;

import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.event.UserSettingsChangedEvent;
import de.gwtprime.client.event.handler.UserSettingsChangedEventHandler;
import de.gwtprime.client.user.ACLManager;
import de.gwtprime.client.user.ACLManager.PrimeUIElement;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.views.popup.ChangeBankAccountPopup;
import de.gwtprime.client.views.popup.ChangePrivateWithdrawAddress;
import de.gwtprime.client.views.popup.ChangeUserSettingsPopup;
import de.gwtprime.client.views.popup.DepositCoinsPopup;
import de.gwtprime.client.views.popup.WithdrawCoinsPopup;
import de.gwtprime.client.widgets.trading.TransactionOverview;
import de.gwtprime.shared.requestFactory.TransactionProxy;
import de.gwtprime.shared.requestFactory.User2Proxy;
import de.gwtprime.shared.util.AppUtilsShared;

public class UserAccountViewImpl extends Composite implements UserAccountView{

	public static ServiceViewImplUiBinder uiBinder = GWT.create(ServiceViewImplUiBinder.class);
	interface ServiceViewImplUiBinder extends UiBinder<Widget, UserAccountViewImpl> {}

	private String name;
	
	private Presenter presenter;

	PopupPanel popup = new PopupPanel(true);
	@UiField Label emailVal;
	@UiField Label userNameLbl;
	@UiField Label userNameVal;
	@UiField Label balanceBitcoinVal;
	@UiField Button changeAcctounBtn;
	@UiField Button changeBankAccount;
	@UiField Button depositCoinsBtn;
	@UiField Button withdrawCoinsBtn;
	@UiField Button privateWithdrawBtn;
	@UiField SimplePanel transactionOverview;

	public UserAccountViewImpl(){
		initWidget(uiBinder.createAndBindUi(this));

		userNameLbl.setText("Name: ");
	}

	@Override
	public void setPresenter(final Presenter presenter) {
		this.presenter = presenter;
		
		presenter.getClientFactory().getEventBus().addHandler(UserSettingsChangedEvent.TYPE, new UserSettingsChangedEventHandler() {
			
			@Override
			public void onUserSettingsChanged(UserSettingsChangedEvent event) {
				populateUserFields(event.getUser());
			}
		});

		depositCoinsBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				popup.clear();
				popup.setWidget(new DepositCoinsPopup(presenter.getClientFactory()));
				popup.setWidth(AppUtils.popupWidth);
				popup.setGlassEnabled(true);
				popup.center();
			}
		});

		withdrawCoinsBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				new WithdrawCoinsPopup(presenter.getClientFactory()).center();
			}
		});
		
		privateWithdrawBtn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				new ChangePrivateWithdrawAddress(presenter.getClientFactory()).center();
			}
		});
	}

	public Presenter getPresenter(){
		return presenter;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName(){
		return name;
	}

	

	@Override
	public void setUser(final User2Proxy user) {

		if(user==null) return;

		changeAcctounBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {


				popup.clear();
				popup.setWidget(new ChangeUserSettingsPopup(presenter.getClientFactory()));
				popup.setWidth(AppUtils.popupWidth);

				popup.setGlassEnabled(true);
				popup.center();

			}
		});


		changeBankAccount.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				popup.clear();
				popup.setWidget(new ChangeBankAccountPopup(presenter.getClientFactory()));
				popup.setWidth(AppUtils.popupWidth);

				popup.setGlassEnabled(true);
				popup.center();

			}
		});

		changeAcctounBtn.setVisible(ACLManager.hasUserAccess(user, PrimeUIElement.EditProfile));

		populateUserFields(user);
	}

	private void populateUserFields(final User2Proxy user) {
		
		if(user==null) return;
		
		String newBalance = user.getBalanceBitcoin() != null ? user.getBalanceBitcoin().toString() : "0";
		if(!AppUtilsShared.isNullOrEmpty(balanceBitcoinVal.getText()) && !newBalance.equals(balanceBitcoinVal.getText())){
			balanceBitcoinVal.addStyleName("valueChanged");
		}
		
		balanceBitcoinVal.setText(newBalance);
		emailVal.setText(user.getEmail());

		userNameVal.setText(user.getName());
		
		userNameVal.setText(user.getName());
	}

	@Override
	public void setTransactionOverview(Set<TransactionProxy> set) {
		transactionOverview.clear();
		transactionOverview.add(new TransactionOverview(set));
	}
}
